package com.tool.imp;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.kingdee.util.db.SQLUtils;
import com.tool.DBConnector;
import com.tool.DataCenterVO;

/**
 * VO基类
 * 
 * @author Evan.Hu
 * 
 */
public abstract class BaseVO {
	public BaseVO() {
		values = new HashMap<String, Object>();
	}

	protected Map<String, Object> values;

	/**
	 * 保存数据库中的数据表名
	 * 
	 * @return
	 */
	public abstract String getTableName();

	/**
	 * PK 字段
	 * 
	 * @return
	 */
	public abstract String getPKField();

	public abstract Map<String, FieldVO> getFieldMap();

	/**
	 * VO保存
	 */
	public boolean saveVO(DataCenterVO dbVo) {
		int insertCount = 0;
		StringBuilder fieldSql = new StringBuilder();
		for (FieldVO field : getFieldMap().values()) {
			if (field.getType().equals(Integer.class)) {
				fieldSql.append(fieldSql.length() > 0 ? "," : "").append(field.getSqlName());
				insertCount++;
			} else if (field.getType().equals(BigDecimal.class)) {
				fieldSql.append(fieldSql.length() > 0 ? "," : "").append(field.getSqlName());
				insertCount++;
			} else if (field.getType().equals(String.class)) {
				fieldSql.append(fieldSql.length() > 0 ? "," : "").append(field.getSqlName());
				insertCount++;
			} else if (field.getType().equals(Date.class)) {
				fieldSql.append(fieldSql.length() > 0 ? "," : "").append(field.getSqlName());
				insertCount++;
			} else if (field.getType().equals(EntryVO.class)) {
				// 保存子记录集
				final List<?> entries = getList(field.getName());
				System.out.println("entries is :::"+entries);
				if (!entries.isEmpty()) {
					((EntryVO) entries.get(0)).batchSaveVO(dbVo, entries);
				}
			} else {
				// 不做保存的数据类型
				continue;
			}
		}
		if (insertCount == 0) {
			return false;
		}
		StringBuilder insertSql = new StringBuilder();
		insertSql.append(" insert into ").append(getTableName()).append(" (").append(fieldSql).append(") values(?");
		for (int i = 1; i < insertCount; i++) {
			insertSql.append(",?");
		}
		insertSql.append(")");

		// 开始保存
		int index = 1;
		Connection conn = null;
		PreparedStatement prepStmt = null;
		try {
			conn = DBConnector.getJDBCConnection(dbVo);
			prepStmt = conn.prepareStatement(insertSql.toString());

			for (FieldVO field : getFieldMap().values()) {
				if (field.getType().equals(Integer.class)) {
					prepStmt.setInt(index, getInteger(field.getName()));
					index++;
				} else if (field.getType().equals(BigDecimal.class)) {
					prepStmt.setBigDecimal(index, getBigDecimal(field.getName()));
					index++;
				} else if (field.getType().equals(String.class)) {
					prepStmt.setString(index, getString(field.getName()));
					index++;
				} else if (field.getType().equals(Date.class)) {
					final Date utilDate = getDate(field.getName());
					java.sql.Date sqlDate = null;
					if (utilDate != null) {
						sqlDate = new java.sql.Date(utilDate.getTime());
					}
					prepStmt.setDate(index, sqlDate);
					index++;
				}
			}
			prepStmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			SQLUtils.cleanup(prepStmt, conn);
		}

		return true;
	}

	/**
	 * VO批量保存
	 */
	public int[] batchSaveVO(DataCenterVO dbVo, List vos) {
		int insertCount = 0;
		StringBuilder fieldSql = new StringBuilder();
		for (FieldVO field : getFieldMap().values()) {
			if (field.getType().equals(Integer.class)) {
				fieldSql.append(fieldSql.length() > 0 ? "," : "").append(field.getSqlName());
				insertCount++;
			} else if (field.getType().equals(BigDecimal.class)) {
				fieldSql.append(fieldSql.length() > 0 ? "," : "").append(field.getSqlName());
				insertCount++;
			} else if (field.getType().equals(String.class)) {
				fieldSql.append(fieldSql.length() > 0 ? "," : "").append(field.getSqlName());
				insertCount++;
			} else if (field.getType().equals(Date.class)) {
				fieldSql.append(fieldSql.length() > 0 ? "," : "").append(field.getSqlName());
				insertCount++;
			} else if (field.getType().equals(BillVO.class)) {
				// 如果为父节点，需要保存入库
				fieldSql.append(fieldSql.length() > 0 ? "," : "").append(field.getSqlName());
				insertCount++;
			} else {
				// 不做保存的数据类型
				continue;
			}
		}
		if (insertCount == 0) {
			return null;
		}
		StringBuilder insertSql = new StringBuilder();
		insertSql.append(" insert into ").append(getTableName()).append(" (").append(fieldSql).append(") values(?");
		for (int i = 1; i < insertCount; i++) {
			insertSql.append(",?");
		}
		insertSql.append(")");
		
		System.out.println("插入的SQL语句insertSql是:"+insertSql);

		// 开始保存
		Connection conn = null;
		PreparedStatement prepStmt = null;
		try {
			conn = DBConnector.getJDBCConnection(dbVo);
			prepStmt = conn.prepareStatement(insertSql.toString());

			for (int i = 0, size = vos.size(); i < size; i++) {
				int index = 1;
				BaseVO vo = (BaseVO) vos.get(i);

				for (FieldVO field : getFieldMap().values()) {
					if (field.getType().equals(Integer.class)) {
						prepStmt.setInt(index, vo.getInteger(field.getName()));
						index++;
					} else if (field.getType().equals(BigDecimal.class)) {
						prepStmt.setBigDecimal(index, vo.getBigDecimal(field.getName()));
						index++;
					} else if (field.getType().equals(String.class)) {
						prepStmt.setString(index, vo.getString(field.getName()));
						index++;
					} else if (field.getType().equals(Date.class)) {
						final Date utilDate = vo.getDate(field.getName());
						java.sql.Date sqlDate = null;
						if (utilDate != null) {
							sqlDate = new java.sql.Date(utilDate.getTime());
						}
						if(sqlDate!=null)
							prepStmt.setDate(index, sqlDate);
						index++;
					} else if (field.getType().equals(BillVO.class)) {
						// 如果为父节点等基础资料，需要取出PK值保存入库
						final BillVO baseDataVO = (BillVO) vo.get(field.getName());
						prepStmt.setString(index, baseDataVO.getString(baseDataVO.getPKField()));
						index++;
					}
				}
				prepStmt.addBatch();
			}
			return prepStmt.executeBatch();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			SQLUtils.cleanup(prepStmt, conn);
		}

		return null;
	}

	public void setString(String key, String value) {
		values.put(key, value);
	}

	public void setInteger(String key, Integer value) {
		values.put(key, value);
	}

	public void setBigDecimal(String key, BigDecimal value) {
		values.put(key, value);
	}

	public void setDate(String key, Date value) {
		values.put(key, value);
	}

	public String getString(String key) {
		return (String) values.get(key);
	}

	public Integer getInteger(String key) {
		return (Integer) values.get(key);
	}

	public BigDecimal getBigDecimal(String key) {
		return (BigDecimal) values.get(key);
	}

	public Date getDate(String key) {
		return (Date) values.get(key);
	}

	@SuppressWarnings("unchecked")
	public void setList(String key, List value) {
		values.put(key, value);
	}

	@SuppressWarnings("unchecked")
	public List<?> getList(String key) {
		return (List) values.get(key);
	}

	protected void put(String key, Object value) {
		values.put(key, value);
	}

	protected Object get(String key) {
		return values.get(key);
	}
}
